package manage.util;


import java.io.*;

public class LogUtil {

    public static boolean createFile(String fileName){
        Boolean bool = false;
        String path = "D:/Bishe/Log/";
        String fileTemp = path+fileName+".txt";
        File file = new File(fileTemp);
        try{
            if(!new File(path).exists())   {
                new File(path).mkdirs();
            }
            if(!file.exists()){
                file.createNewFile();
                bool =true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bool;
    }

    public static boolean writeFileLog(String fileName,String txt) {
        LogUtil.createFile(fileName);
        boolean bool=false;
        String filein = txt+"\r\n";//新写入的行，换行
        String temp  = "";
        String path = "D:/Bishe/Log/";
        String fileTemp = path+fileName+".txt";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos  = null;
        PrintWriter pw = null;
        try {
            File file = new File(fileTemp);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();

            //文件原有内容
            for(int i=0;(temp =br.readLine())!=null;i++){
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            buffer.append(filein);

            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            //不要忘记关闭
            try {
                if (pw != null) {
                    pw.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bool;
    }
}
